package com.zhongwang.mms.module.half.service;

import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.gen.entity.HOrder;
import com.zhongwang.mms.module.half.dao.HInventoryDao;
import com.zhongwang.mms.module.half.model.HInBillModel;
import com.zhongwang.mms.module.half.model.HInventoryQueryModel;
import com.zhongwang.mms.module.half.model.HOrderDetailModel;
import com.zhongwang.mms.module.half.model.PrintListModel;
import com.zhongwang.mms.module.system.log.LogService;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:外购外协出库
 * @author: th
 * @time: 2020/3/28 8:05
 */
@Service
public class OutOfBuyWarService {

    @Autowired
    private HInventoryDao hInventoryDao;
    @Autowired
    private HOrderService hOrderService;
    @Autowired
    private HOrderDetailService hOrderDetailService;
    @Autowired
    private HInventoryService hInventoryService;
    @Autowired
    private HBasicService hBasicService;
    @Autowired
    private LogService logService;

    //查询库存列表
    public List<HOrderDetailModel> outOfFinishWar(HInventoryQueryModel query) {

        return hInventoryDao.findHInventoryByQuery(query);
    }

    @Transactional
    public JsonResult saveHInBillModel(HInBillModel hInBillModel) {

        JsonResult jsonResult = new JsonResult();
        List<String> result = new ArrayList<>();
        PrintListModel printListModel = new PrintListModel();
        //验证数量是否可以出库
        String msg = "";  // 返回提示信息

        // 1 校验保存数据可用性、存在不满足条件数据 整单不允许保存
        msg = hBasicService.checkOutValidate(hInBillModel, msg);
        if (msg.length() > 0) {
            msg = msg + "<br>可用在库库存量不足，保存失败！";
            jsonResult.setMsg(msg);
            jsonResult.setType(JsonResult.ResultType.ERROR);
            jsonResult.setData("保存失败");
            return jsonResult;
        }
        //验证出库时间是否为空
        for (var i = 0; i < hInBillModel.getInsertData().size(); i++) {
            if(hInBillModel.getInsertData().get(i).getOdaStorageDate() == null){
                result.add("\n第" + (i + 1) + "行" + " 出库日期 为空！");
            }
            if(hInBillModel.getInsertData().get(i).getOdaAvaNum() <= 0){
                result.add("\n第" + (i + 1) + "行" + " 数量 必须大于0！");
            }
        }
        if (result.size() > 0) {
            result.add("\n单据保存失败，请先维护以上物料信息！");
            printListModel.setList(result);
            printListModel.setId(hInBillModel.getFormData().getOdrId());
            jsonResult.setData(printListModel);
            return jsonResult;
        }
        hInBillModel.getFormData().setOdrType(2);//1入库2出库
        hInBillModel.getFormData().setOdrStatus(0);//1调拨0其他

        //保存主单信息
        HOrder hOrderForm = hOrderService.saveOutHOrder(hInBillModel);

        HOrderDetailModel hOrderDetailModel = new HOrderDetailModel();
        //遍历表单内容
        for (var i = 0; i < hInBillModel.getInsertData().size(); i++) {

            //获取表单明细
            hOrderDetailModel = hInBillModel.getInsertData().get(i);
            //获取主单ID
            hOrderDetailModel.setOdrId(hInBillModel.getFormData().getOdrId());
            //物料分类
            hOrderDetailModel.setOdaMatType(2);//1是外购件、2是外协件、3是制作件
            //物料编码
            hOrderDetailModel.setOdaMatCode("1111111111");
            //红冲标识
            hOrderDetailModel.setOdaRedFlag(0);
            //保存表单明细
            hOrderDetailService.saveHOrderDetail(hOrderDetailModel);
            //保存库存信息
            hInventoryService.saveOutHInventory(hOrderDetailModel);

        }
        result.add("单据保存成功！");
        logService.saveLog("【成品库】外协件出库","出库单据编码：" +hOrderForm.getOdrCode());
        printListModel.setList(result);
        printListModel.setId(hInBillModel.getFormData().getOdrId());
        jsonResult.setData(printListModel);
        jsonResult.setType(JsonResult.ResultType.SUCCESS);
        return jsonResult;
    }
}
